/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

#include "getscreenshot.h"
#include <QGuiApplication>

GetScreenShot::GetScreenShot()
{
    if (QGSettings::isSchemaInstalled("org.mate.background")) {
        m_backgroundGSettings = new QGSettings("org.mate.background");
        setBackground(m_backgroundGSettings->get("pictureFilename").toString());
        connect(m_backgroundGSettings, &QGSettings::changed, [this](const QString &key) {
            if (key == "pictureFilename") {
                setBackground(m_backgroundGSettings->get("pictureFilename").toString());
            }
        });
    }
}
void GetScreenShot::paint(QPainter *painter)
{
    painter->setRenderHint(QPainter::Antialiasing,true);
    m_screen = QGuiApplication::primaryScreen();
    if(m_flag) {
        painter->drawPixmap(0,0,width(),height(),m_screen->grabWindow(0));
    }
}
void GetScreenShot::getScreen()
{
    m_screen = QGuiApplication::primaryScreen();
    m_flag = true;
    update();
}

QString GetScreenShot::background() const
{
    return m_background;
}

void GetScreenShot::setBackground(const QString &background)
{
    if (m_background != background) {
        m_background = background;
        Q_EMIT backgroundChanged();
    }
}
